﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Server.Search.Administration;
using Microsoft.SharePoint;
using Microsoft.Office.Server.Search.Query;
using Microsoft.SharePoint.Utilities;
using SPPivotViewer.Utility;

namespace SPPivotViewer.HttpHandler.Internal
{
    public class SPSearchFactory
    {
        public Collection MakeCollection(CollectionRequestContext context, string collectionFileName)
        {
            Collection _collection = new Collection();
            string strKeyword=context.Query["k"];
            if(string.IsNullOrEmpty(strKeyword))
                return _collection;
            SearchServiceApplicationProxy searchProxy =
            (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(SPContext.Current.Site));


            KeywordQuery keyword = new KeywordQuery(searchProxy);

            keyword.QueryText = strKeyword;
            keyword.EnableNicknames = true;
            keyword.EnablePhonetic = true;
            keyword.ResultsProvider = SearchProvider.Default;
            keyword.ResultTypes = ResultType.RelevantResults | ResultType.RefinementResults;
            keyword.EnableSpellcheck = SpellcheckMode.On;
            keyword.EnableStemming = true;
            string strMaxItem = PV4SPSettings.GetValue(SPContext.Current.Web, PV4SPSettings.cstMaxItem);
            int _iMaxItem = 0;
            keyword.RowsPerPage = keyword.RowLimit = int.TryParse(strMaxItem, out _iMaxItem) ? _iMaxItem : 300; 
            //keyword.RowsPerPage = keyword.RowLimit = 150;

            ResultTableCollection rtc=keyword.Execute();
            ResultTable rt = rtc[ResultType.RelevantResults];
            while (rt.Read())
            {
                
                List<Facet> lstFct = new List<Facet>();
                string strName=rt["Title"].ToString();
                string strUrl=rt["Url"].ToString();
                string strContentClass = rt["ContentClass"].ToString();
                bool bIsDocument = bool.Parse(rt["IsDocument"].ToString());
                string strDescription = rt["HitHighlightedSummary"].ToString();
                Facet fctTitle =new Facet("Title", FacetType.Text, rt["Title"]);
                fctTitle.ShowInFacetPane = false;
                lstFct.Add(fctTitle);
                lstFct.Add(new Facet("Author", FacetType.Text, rt["Author"]));
                lstFct.Add(new Facet("Size", FacetType.Number, rt["Size"]));
                lstFct.Add(new Facet("Created", FacetType.Text, rt["Write"]));
                lstFct.Add(new Facet("Site", FacetType.Text, rt["SiteName"]));
                lstFct.Add(new Facet("Document Type", FacetType.Text, rt["FileExtension"]));

                string strimageUrl = "";
                if (bIsDocument)
                    strimageUrl = strUrl;
                else
                    strimageUrl = IconForNonDocument(strUrl);

                _collection.AddItem(strName,strUrl,strDescription,new ItemImage(new Uri(strUrl)),lstFct.ToArray());
            }
            return _collection;
        }

        private string IconForNonDocument(string strUrl)
        {
            string strIcon = SPUtility.MapToIcon(SPContext.Current.Web, strUrl, string.Empty,IconSize.Size32);

            string strIconPath = SPContext.Current.Web.Url + "/_layouts/images/" + strIcon;
            return strIconPath;
        }
    }
}
